package com.vrv.common.adapter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.vrv.netmgr.R;

/**
 * <ul>
 * 定制可扩展列表导航适配器
 * <li>三级导航没有子节点的时候显示">"箭头</li>
 * <li>三级导航有子节点（四级导航）的时候显示"V"箭头</li>
 * </ul>
 * 
 * @author 曹保利
 * 
 */
public class VRVExpandableListAdapter extends BaseExpandableListAdapter {

	private List<Map<String, String>> rawGroups = new ArrayList<Map<String, String>>();
	private List<List<Map<String, String>>> rawChilds = new ArrayList<List<Map<String, String>>>();
	private int groupLayout;
	private int childLayout;

	// private String[] groupDataBinderKeys;
	// private String[] childDataBinderKeys;
	// private int[] groupViewKeys;
	// private int[] childViewKeys;

	private Context mContext;

	public VRVExpandableListAdapter(Context mContext, List<Map<String, String>> group, int groupLayout, List<List<Map<String, String>>> child, int childLayout) {
		super();
		this.mContext = mContext;
		this.rawGroups = group;
		this.rawChilds = child;

		this.groupLayout = groupLayout;
		this.childLayout = childLayout;
	}

	@Override
	public int getGroupCount() {
		return rawGroups.size();
	}

	@Override
	public int getChildrenCount(int groupPosition) {
		return rawChilds.get(groupPosition).size();
	}

	@Override
	public Object getGroup(int groupPosition) {
		return rawGroups.get(groupPosition);
	}

	@Override
	public Object getChild(int groupPosition, int childPosition) {
		return rawChilds.get(groupPosition).get(childPosition);
	}

	@Override
	public long getGroupId(int groupPosition) {
		return groupPosition;
	}

	@Override
	public long getChildId(int groupPosition, int childPosition) {
		return childPosition;
	}

	@Override
	public boolean hasStableIds() {
		return true;
	}

	@SuppressWarnings("unchecked")
	@Override
	public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

		// 实例化布局文件
		RelativeLayout glayout = (RelativeLayout) LayoutInflater.from(mContext).inflate(groupLayout, null);
		ImageView iconView = (ImageView) glayout.findViewById(R.id.arrow_icon);

		if (getChildrenCount(groupPosition) > 0) {
			iconView.setImageResource(R.drawable.down_arrow);
		} else {
			iconView.setImageResource(R.drawable.right_arrow);
		}

		TextView tv = (TextView) glayout.findViewById(R.id.groupLabel);
		Map<String, String> varMap = (Map<String, String>) this.getGroup(groupPosition);
		tv.setText(varMap.get("group").toString());
		return glayout;
	}

	@SuppressWarnings("unchecked")
	@Override
	public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
		LinearLayout clayout = (LinearLayout) LayoutInflater.from(mContext).inflate(childLayout, null);
		TextView tv = (TextView) clayout.findViewById(R.id.childLabel);
		Map<String, String> varMap = (Map<String, String>) getChild(groupPosition, childPosition);
		tv.setText(varMap.get("child").toString());
		return clayout;
	}

	@Override
	public boolean isChildSelectable(int groupPosition, int childPosition) {
		return true;
	}

}